package org.xtframe.entity;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.xtframe.bean.SystemConfig;
import org.xtframe.dao.DataBaseDao;
import org.xtframe.util.CommonUtil;
import org.xtframe.util.SpringUtil;
import org.xtframe.util.StringUtil;
import org.xtframe.util.SystemConfigUtil;

public class UserData {
	
	private final Log logger = LogFactory.getLog(getClass());
	
	private DataBaseDao dataBaseDao;
	private SystemConfig systemConfig;

	// 服务器数据
	private ServerData serverData;
	// 用户
	private User user;
	// 对象创建时间
	private long createTime;
	// 菜单list
	private ArrayList<Integer> menuList;
	// 机构
	private Organ organ;
	// 岗位
	private Job job;
	// 用户所属角色
	private ArrayList<String> roleList;
	// 用户授权权
	private ArrayList<Map<String, String>> optRoleList;
	// 用户机构数据
	private Map<String, String> organData;
	
	public UserData(ServerData serverData) {
		dataBaseDao = (DataBaseDao) SpringUtil.getBean("dataBaseDao");
		systemConfig = SystemConfigUtil.getSystemConfig();
		
		this.serverData = serverData;
		user = new User();
		createTime = System.currentTimeMillis();
		organ = new Organ();
		job = new Job();
		menuList = new ArrayList<Integer>();
		roleList = new ArrayList<String>();
		optRoleList = null;
		organData = new TreeMap<String, String>();
	}
	
	public boolean init(String un, String pd) {
		try {
			String strSql = CommonUtil.createSql(systemConfig.getUserInitInfoSql(), un, pd);
			List<Map<String, Object>> result = dataBaseDao.executeQuery(strSql, null);
			if(result==null || result.size()<=0) return false;
			
			Map<String, Object> obj = result.get(0);
			user.setUserId(StringUtil.toString(obj.get("userid"), "").trim());
			user.setUserName(StringUtil.toString(obj.get("username"), ""));
			user.setPwd(StringUtil.toString(obj.get("pwd"), ""));
			user.setRealName(StringUtil.toString(obj.get("realname"), ""));
			user.setSex(StringUtil.toString(obj.get("sex"), ""));
			user.setBirthday(StringUtil.toString(obj.get("birthday"), ""));
			user.setIdCard(StringUtil.toString(obj.get("idcard"), ""));
			user.setPhoto(StringUtil.toString(obj.get("photo"), ""));
			user.setAddress(StringUtil.toString(obj.get("address"), ""));
			user.setTel(StringUtil.toString(obj.get("tel"), ""));
			user.setMobile(StringUtil.toString(obj.get("mobile"), ""));
			user.seteMail(StringUtil.toString(obj.get("email"), ""));
			user.setRemark(StringUtil.toString(obj.get("remark"), ""));
			user.setOrganId(Integer.valueOf(StringUtil.toString(obj.get("organid"), "")));
			user.setJobId(Integer.valueOf(StringUtil.toString(obj.get("jobid"), "")));
			user.setOrderNo(Integer.valueOf(StringUtil.toString(obj.get("orderno"), "")));
			user.setState(Integer.valueOf(StringUtil.toString(obj.get("state"), "")));
			
			strSql = CommonUtil.createSql(systemConfig.getUserInitOrganSql(), user.getUserId());
			result = dataBaseDao.executeQuery(strSql, null);
			if(result==null || result.size()<=0) return false;
			
			obj = result.get(0);
			organ.setOrganId(Integer.valueOf(StringUtil.toString(obj.get("organid"), "")));
			organ.setOrganName(StringUtil.toString(obj.get("organname"), ""));
			organ.setAtOrganId(StringUtil.toString(obj.get("atorganid"), ""));
			organ.setOrganLevel(Integer.valueOf(StringUtil.toString(obj.get("organlevel"), "")));
			organ.setOrderNo(Integer.valueOf(StringUtil.toString(obj.get("orderno"), "")));
			organ.setRemark(StringUtil.toString(obj.get("remark"), ""));
			
			strSql = CommonUtil.createSql(systemConfig.getUserInitJobSql(), user.getUserId());
			result = dataBaseDao.executeQuery(strSql, null);
			if(result!=null && result.size()>0){
				obj = result.get(0);
				job.setJobId(Integer.valueOf(StringUtil.toString(obj.get("jobid"), "")));
				job.setOrganId(Integer.valueOf(StringUtil.toString(obj.get("organid"), "")));
				job.setJobName(StringUtil.toString(obj.get("jobname"), ""));
				job.setAtJobId(StringUtil.toString(obj.get("atjobid"), ""));
				job.setJobLevel(Integer.valueOf(StringUtil.toString(obj.get("joblevel"), "")));
				job.setOrderNo(Integer.valueOf(StringUtil.toString(obj.get("orderno"), "")));
				job.setRemark(StringUtil.toString(obj.get("remark"), ""));
			}
			
			roleList.clear();
			strSql = CommonUtil.createSql(systemConfig.getUserInitRoleSql(), user.getUserId());
			List<String> list = dataBaseDao.executeQuery(strSql, 1, null);
			if(list != null){
				for(String str : list){
					roleList.add(str.trim());
				}
			}
			
			menuList.clear();
			for(String roleId : roleList){
				Role role = serverData.getRole(roleId);
				menuList.addAll(role.getMenu());
			}
			menuList = new ArrayList<Integer>(new HashSet<Integer>(menuList));
			
			return true;
		} catch (NumberFormatException e) {
			logger.error(e.toString());
		}
		return false;
	}
	
	public boolean isInSQL(String sqlId){
		boolean result = false;
		for(String roleId : roleList){
			Role role = serverData.getRole(roleId);
			if(role.isInSQL(sqlId)){
				result = true;
				break;
			}
		}
		return result;
	}
	
	public boolean isInLogic(String logicId){
		boolean result = false;
		for(String roleId : roleList){
			Role role = serverData.getRole(roleId);
			if(role.isInLogic(logicId)){
				result = true;
				break;
			}
		}
		return result;
	}
	
	public boolean isInRes(String resId){
		boolean result = false;
		for(String roleId : roleList){
			Role role = serverData.getRole(roleId);
			if(role.isInRes(resId)){
				result = true;
				break;
			}
		}
		return result;
	}
	
	public ArrayList<Map<String, String>> getOptRoleList(){
		if(optRoleList != null) return optRoleList;
		String strSql = CommonUtil.createSql(systemConfig.getUserInitOptroleSql(), user.getUserId());
		List<String> result = dataBaseDao.executeQuery(strSql, 1, null);
		optRoleList = serverData.getOptRoleList((ArrayList<String>) result);
		return optRoleList;
	}
	
	public String getOrganDataValue(String dataName){
		String dataValue = organData.get(dataName);
		if(dataValue != null) return dataValue;
		String strSql = CommonUtil.createSql(systemConfig.getUserOrganDataSql(), dataName, user.getOrganId());
		dataValue = dataBaseDao.findFirst(strSql, 1);
		if(dataValue != null){
			organData.put(dataName, dataValue);
		} else {
			dataValue = "";
		}
		return dataValue;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public ServerData getServerData() {
		return serverData;
	}

	public void setServerData(ServerData serverData) {
		this.serverData = serverData;
	}

	public long getCreateTime() {
		return createTime;
	}

	public void setCreateTime(long createTime) {
		this.createTime = createTime;
	}

	public ArrayList<Integer> getMenuList() {
		return menuList;
	}

	public void setMenuList(ArrayList<Integer> menuList) {
		this.menuList = menuList;
	}

	public Organ getOrgan() {
		return organ;
	}

	public void setOrgan(Organ organ) {
		this.organ = organ;
	}

	public Job getJob() {
		return job;
	}

	public void setJob(Job job) {
		this.job = job;
	}

	public ArrayList<String> getRoleList() {
		return roleList;
	}

	public void setRoleList(ArrayList<String> roleList) {
		this.roleList = roleList;
	}
}
